package com.vn.toaa.karaoke.ui;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.vn.toaa.karaoke.database.DatabaseManager;
import com.vn.toaa.karaokecodelist.R;

public class AlphabetListAdapter extends BaseAdapter {

    public static abstract class Row {}
    
    public static final class Section extends Row {
        public final String text;

        public Section(String text) {
            this.text = text;
        }
    }
    
    public static final class Item extends Row {
        public final int mSongId;
        public int mCollection;
        public final String mSongName;
        public final String mSongLyric;
        public boolean mIsFavoriteAdded = false;

        public Item(int songId, String songName, String songLyric, boolean isFavoriteAdded) {
            this.mSongId = songId;
            this.mSongName = songName;
            this.mSongLyric = songLyric;
            this.mIsFavoriteAdded = isFavoriteAdded;
        }

		public Item(int mSongId, int mCollection, String mSongName, String mSongLyric, boolean mIsFavoriteAdded) {
			super();
			this.mSongId = mSongId;
			this.mCollection = mCollection;
			this.mSongName = mSongName;
			this.mSongLyric = mSongLyric;
			this.mIsFavoriteAdded = mIsFavoriteAdded;
		}
        
    }
    
    private List<Row> rows;
    
    public void setRows(List<Row> rows) {
        this.rows = rows;
    }

    @Override
    public int getCount() {
        return rows.size();
    }

    @Override
    public Row getItem(int position) {
        return rows.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public int getViewTypeCount() {
        return 2;
    }
    
    @Override
    public int getItemViewType(int position) {
        if (getItem(position) instanceof Section) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        
        if (getItemViewType(position) == 0) { // Item
        	ViewHolder viewHolder = null;
            if (view == null) {
                LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = (LinearLayout) inflater.inflate(R.layout.row_song_info, parent, false); 
                viewHolder = new ViewHolder();
                viewHolder.mTvSongID = (TextView) view.findViewById(R.id.textview_song_id);
                viewHolder.mTvSongLyric = (TextView) view.findViewById(R.id.textview_song_lyric);
                viewHolder.mTvSongName = (TextView) view.findViewById(R.id.textview_song_name);
                viewHolder.mIvFavorite = (ImageView) view.findViewById(R.id.imageview_favorite);
                
                view.setTag(viewHolder);
            } else {
				viewHolder = (ViewHolder) view.getTag();
			}
            
            final Item item = (Item) getItem(position);
            viewHolder.mTvSongID.setText(item.mSongId + "");
            viewHolder.mTvSongName.setText(item.mSongName);
            viewHolder.mTvSongLyric.setText(item.mSongLyric);
            item.mIsFavoriteAdded = DatabaseManager.getInstance().isExistInFavorite(item.mSongId);
			viewHolder.mIvFavorite.setActivated(item.mIsFavoriteAdded);
			viewHolder.mIvFavorite.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					item.mIsFavoriteAdded = !item.mIsFavoriteAdded;
					v.setActivated(item.mIsFavoriteAdded);
					if (item.mIsFavoriteAdded) {
						try {
							DatabaseManager.getInstance().insertFavoriteSong(item.mSongId);
						} catch (NumberFormatException e) {
							e.printStackTrace();
						}
					} else {
						try {
							DatabaseManager.getInstance().deleteFavoriteSong(item.mSongId);
						} catch (NumberFormatException e) {
							e.printStackTrace();
						}
					}
				}
			});
        } else { // Section
            if (view == null) {
                LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = (LinearLayout) inflater.inflate(R.layout.row_section, parent, false);  
            }
            
            Section section = (Section) getItem(position);
            TextView textView = (TextView) view.findViewById(R.id.textView1);
            textView.setText(section.text);
        }
        
        return view;
    }
    
    static class ViewHolder {
    	TextView mTvSongID;
    	TextView mTvSongLyric;
    	TextView mTvSongName;
    	ImageView mIvFavorite;
    }

}
